package demo.hdz.mediacodecdecode.fileexplorer;

import android.widget.BaseAdapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.List;

import demo.hdz.mediacodecdecode.Logger;
import demo.hdz.mediacodecdecode.R;

public class FileInfosAdapter extends BaseAdapter {

    private static final String TAG = "FileInfosAdapter";

    private Bitmap mFolderIcon;
    private Bitmap mFileIcon;

    private LayoutInflater mInflater;
    private List<String> mItems;
    private List<String> mPaths;

    private WeakReference<Context> mWeakContext = new WeakReference<>(null);




    public FileInfosAdapter(Context context, List<String> items, List<String> paths) {
        mInflater = LayoutInflater.from(context);
        mItems = items;
        mPaths = paths;
        mWeakContext = new WeakReference<>(context);

        mFileIcon        = BitmapFactory.decodeResource(context.getResources(), R.drawable.file);
        mFolderIcon      = BitmapFactory.decodeResource(context.getResources(), R.drawable.folder);
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemContent itemContent;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.file_infos_item, null);
            itemContent = new ItemContent();
            itemContent.name = (TextView) convertView.findViewById(R.id.name);
            itemContent.path = (TextView) convertView.findViewById(R.id.path);
            itemContent.icon = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(itemContent);
        } else {
            itemContent = (ItemContent) convertView.getTag();
        }
        String path = mPaths.get(position);
        Logger.d(TAG+",getView: path = " + path + ", position = " + position);
        File file = new File(path);

        itemContent.name.setText(file.getName());
        itemContent.path.setText(file.getPath());
        if (file.isDirectory()) {
            itemContent.icon.setImageBitmap(mFolderIcon);
        } else {
            itemContent.icon.setImageBitmap(mFileIcon);
            setFileIcon(mWeakContext.get(), file.getName(), itemContent);
        }

        return convertView;
    }

    private String getFileSuffix(String fileName) {
        int iPos = fileName.lastIndexOf(".");
        if (iPos > 0) {
            return fileName.substring(iPos+1);
        } else {
            return  "";
        }
    }

    private void setFileIcon(Context context, String fileName, ItemContent itemContent) {
        if (getFileSuffix(fileName).toLowerCase().equals("txt")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.text);
        } else if (getFileSuffix(fileName).toLowerCase().equals("pdf")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.pdf);
        } else if (getFileSuffix(fileName).toLowerCase().equals("doc")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.doc);
        } else if (getFileSuffix(fileName).toLowerCase().equals("jpeg")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.jpeg);
        } else if (getFileSuffix(fileName).toLowerCase().equals("jpg")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.jpg);
        } else if (getFileSuffix(fileName).toLowerCase().equals("png")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.png);
        } else if (getFileSuffix(fileName).toLowerCase().equals("mp3")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.mp3);
        } else if (getFileSuffix(fileName).toLowerCase().equals("mp4")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.mp4);
        } else if (getFileSuffix(fileName).toLowerCase().equals("avi")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.avi);
        } else if (getFileSuffix(fileName).toLowerCase().equals("wav")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.wav);
        } else if (getFileSuffix(fileName).toLowerCase().equals("rmvb")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.rmvb);
        } else if (getFileSuffix(fileName).toLowerCase().equals("mov")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.mov);
        } else if (getFileSuffix(fileName).toLowerCase().equals("rar") ||
                getFileSuffix(fileName).toLowerCase().equals("zip") ||
                getFileSuffix(fileName).toLowerCase().equals("img")) {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.zip);
        } else {
            mFileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.file);
        }
        itemContent.icon.setImageBitmap(mFileIcon);
    }

    private class ItemContent {
        TextView name;
        TextView path;
        ImageView icon;
    }
}
